#region File Description
//  XAEnima 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: This component handles the Keyboard inputs.
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion


namespace XAEnima.Core.GameManager
{
    public delegate void PressedEventHandler();

    public class InputManager : Microsoft.Xna.Framework.GameComponent, IInputManager
    {
        KeyboardState kstate,oldstate;
        public event PressedEventHandler EnterPressed;
        public event PressedEventHandler EnterReleased;
        public event PressedEventHandler EscapePressed;
        public event PressedEventHandler EscapeReleased;
        public event PressedEventHandler UpPressed;
        public event PressedEventHandler DownPressed;
        public event PressedEventHandler LeftPressed;
        public event PressedEventHandler RightPressed;
        public event PressedEventHandler TabPressed;
        public event PressedEventHandler SPressed;
        public event PressedEventHandler SReleased;
        public event PressedEventHandler XPressed;
        public event PressedEventHandler XReleased;

        public InputManager(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IInputManager), this);
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            base.Dispose(disposing);
        }
        public override void Initialize()
        {
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            kstate = Keyboard.GetState(PlayerIndex.One);
            if ((oldstate.IsKeyUp(Keys.Enter)) && (kstate.IsKeyDown(Keys.Enter))){
                if (EnterPressed != null)
                    EnterPressed();   
            }
            if ((oldstate.IsKeyDown(Keys.Enter)) && (kstate.IsKeyUp(Keys.Enter)))
            {
                if (EnterReleased != null)
                    EnterReleased();
            }
            if ((oldstate.IsKeyUp(Keys.Escape)) && (kstate.IsKeyDown(Keys.Escape))){
                if (EscapePressed != null)
                    EscapePressed();
            }
            if ((oldstate.IsKeyDown(Keys.Escape)) && (kstate.IsKeyUp(Keys.Escape)))
            {
                if (EscapeReleased != null)
                    EscapeReleased();
            }
            if ((oldstate.IsKeyUp(Keys.Tab)) && (kstate.IsKeyDown(Keys.Tab))){
                if (TabPressed != null)
                    TabPressed();
            }
            if ((oldstate.IsKeyUp(Keys.Left)) && (kstate.IsKeyDown(Keys.Left))){
                if (LeftPressed != null)
                    LeftPressed();
            }
            if ((oldstate.IsKeyUp(Keys.Right)) && (kstate.IsKeyDown(Keys.Right)))
            {
                if (RightPressed != null)
                    RightPressed();
            }
            if ((oldstate.IsKeyUp(Keys.Up)) && (kstate.IsKeyDown(Keys.Up)))
            {
                if (UpPressed != null)
                    UpPressed();
            }
            if ((oldstate.IsKeyUp(Keys.Down)) && (kstate.IsKeyDown(Keys.Down)))
            {
                if (DownPressed != null)
                    DownPressed();
            }
            if ((oldstate.IsKeyUp(Keys.S)) && (kstate.IsKeyDown(Keys.S)))
            {
                if (SPressed != null)
                    SPressed();
            }
            if ((oldstate.IsKeyDown(Keys.S)) && (kstate.IsKeyUp(Keys.S)))
            {
                if (SReleased != null)
                    SReleased();
            }
            if ((oldstate.IsKeyUp(Keys.X)) && (kstate.IsKeyDown(Keys.X)))
            {
                if (XPressed != null)
                    XPressed();
            }
            if ((oldstate.IsKeyDown(Keys.X)) && (kstate.IsKeyUp(Keys.X)))
            {
                if (XReleased != null)
                    XReleased();
            }
            oldstate = kstate;
            base.Update(gameTime);
        }
        public void ClearState() {
            oldstate = new KeyboardState();
            kstate = new KeyboardState();
        }
    }
}
